package cn.yuanqiao.archive.wenjianzhengli.service.impl;

import cn.yuanqiao.archive.wenjianzhengli.domain.Yqdatransdoc;
import cn.yuanqiao.archive.wenjianzhengli.domain.vo.YqdatransdocVo;
import cn.yuanqiao.archive.wenjianzhengli.mapper.YqdatransdocMapper;
import cn.yuanqiao.archive.wenjianzhengli.service.IYqdatransdocService;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static cn.yuanqiao.common.utils.PageUtils.startPage;

/**
 * @author zjj
 * @date 2023/9/19
 */
@Service
public class YqdatransdocServiceImpl implements IYqdatransdocService {
    @Autowired
    private YqdatransdocMapper yqdatransdocMapper;


    @Override
    public List<YqdatransdocVo> selectYqdatransdoc(Integer state, String filename, String starttime, String endtime) {
        String tenantCode = SecurityUtils.getTenantCode();
        List<String> eFileTable = yqdatransdocMapper.getEFileTableName(tenantCode);
        List<YqdatransdocVo> list = new ArrayList<>();
        if(eFileTable !=null && eFileTable.size()>0){
            StringBuilder col = new StringBuilder(" case ");
            StringBuilder filecol = new StringBuilder(" case ");
            StringBuilder whereCol = new StringBuilder();
            for(int i = 0;i<eFileTable.size();i++){
                col.append(" when a"+(i+1)+".doc_id is not null then a"+(i+1)+".doc_name ");
                filecol.append(" when a"+(i+1)+".doc_id is not null then a"+(i+1)+".REAL_DOC_NAME ");
                whereCol.append(" left join "+eFileTable.get(i)+" a"+(i+1)+" on t.doc_id = a"+(i+1)+".doc_id ");
            }
            col.append(" else '' END AS FILE_NAME,");
            filecol.append(" else '' END AS SERVICE_FILE_NAME");
            col.append(filecol);
            startPage();
            list = yqdatransdocMapper.selectYqdatransdoc(new Yqdatransdoc().getTableName(),state,col.toString(),whereCol.toString(),filename,starttime,endtime);
        }
        return list;
    }

    @Override
    public int delYqdatransdoc(Long[] docId) {
        return yqdatransdocMapper.delYqdatransdoc(new Yqdatransdoc().getTableName(),docId);
    }
}
